/**
 * Team members:
 * HengXing Liu 2022229044
 * YiYang Zhang 2022229047
 * ZeJun Liu 2022229092
 */
package project;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * a class to manage logs
 */
public class LogManage {

    static public boolean flag;    //whether to keep the session log
    static public final String savingPath = "SavedData/Logs/";
    static public String filename;

    /**open log
     *
     */
    public void BeaginRecord() {
        this.flag = true;
        System.out.println("logging session to " + filename);

    }

    /**
     * close log
     */
    public void EndRecord() {
        this.flag = false;
        System.out.println("session was logged to "+ filename);
    }

    /**
     *   set log filename
     */
    public void SetFilename(String filename){ this.filename = filename;}

    /**
     * Write str to file
     * @param str
     * @throws Exception
     */
    static public void WriteFile (String str) throws Exception {
        String filepath = savingPath + filename;
        File file = new File(savingPath);
        if(!file.exists()){
            file.mkdirs();
        }
        File log = new File(filepath);
        if(!log.exists()){
            log.createNewFile();
        }
        FileWriter fileWriter = new FileWriter(log,true);
        fileWriter.write(str);
        fileWriter.close();
    }

    /**
     * get all log filenames
     * @return
     */
    public File[] getLogFileName(){
        File file =new File(savingPath);
        if(!file.exists()){
            file.mkdirs();
        }
        File [] files = file.listFiles();
        return files;
    }
}
